//
//  AttentionViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AttentionViewController.h"
#import "Header.h"
#import "FMDBTool.h"
#import "Find.h"
#import "AttentionTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"

@interface AttentionViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource

>
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign)BOOL isAttention;

@end

@implementation AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"关注";
    
}

- (void)viewWillAppear:(BOOL)animated {

    [[FMDBTool shareFMDBTool] openDB];
    [[FMDBTool shareFMDBTool] createTable];
    self.array = [[FMDBTool shareFMDBTool] selectFind];
    NSLog(@"%ld",self.array.count);
    if (self.array.count == 0) {
        [self createView];
    }else {
        [self createTableView];
    }

}





- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    NSLog(@"有值");
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];// tableView 取消下划线
    [self.tableView registerClass:[AttentionTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AttentionTableViewCell class])];
}

- (void)createView {
    BaseView *backView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:backView];
    BaseLabel *label = [[BaseLabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH, 20)];
    [backView addSubview:label];
    label.text = @"关注的人会显示在这里哦~";
    label.textAlignment = NSTextAlignmentCenter;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Find *find = self.array[indexPath.row];
    
    self.isAttention = [[FMDBTool shareFMDBTool] columnExists:find.name inTableWithName:@"channel"];
    NSString *buttonTitle = self.isAttention ? @"已关注" : @"关注";
    
    
    
    AttentionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AttentionTableViewCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // cell 点击不改变颜色
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:find.icon]];
    cell.titleLabel.text = find.name;
    [cell.attentionButton setTitle:buttonTitle forState:UIControlStateNormal];
    [cell.attentionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    if (self.isAttention == NO) {
        cell.attentionButton.backgroundColor =  [UIColor colorWithRed:255 / 255.0 green:211 / 255.0 blue:63 / 255.0 alpha:1];
    }else {
        cell.attentionButton.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:232 / 255.0 blue:232 / 255.0 alpha:1];
    }

    cell.attentionBlock = ^(BaseButton *button) {
        
        if (self.isAttention == NO) {
            button.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:232 / 255.0 blue:232 / 255.0 alpha:1];
            [button setTitle:@"已关注" forState:UIControlStateNormal];
            
            [[FMDBTool shareFMDBTool] insertChannel:find];
        }else {
            button.backgroundColor =   [UIColor colorWithRed:255 / 255.0 green:211 / 255.0 blue:63 / 255.0 alpha:1];
            
            [button setTitle:@"关注" forState:UIControlStateNormal];
            [[FMDBTool shareFMDBTool] deleteChannel:find];
            
        }
        
        self.isAttention = !self.isAttention;
    };

    
    
    
    
    return cell;


}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    Find *find = self.array[indexPath.row];
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    NSString *str = [find.url substringFromIndex:39];
    detailVC.str = str;
    detailVC.findModel = find;
    NSLog(@"%@",str);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
